home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Carousel
/
CAROUSEL.cdr
/
mactosh
/
code
/
p_serlib.sit
/
Serial Library Source Code
/
serial.writeFile.dll.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-07-27
|
9KB
|
319 lines
/***********************************************************************/
/*
/* serial.writeFile.dll.c
/* by Atul Butte
/* Copyright ⌐ 1989 by Microsoft Corporation
/* All Rights Reserved
/*
/* version 1.0
/*
/*
/* This CALL/REGISTER will send a text file through a serial port.
/*
/* Excel usage:
/*
/* = Register( "serial library", "serial.writeFile", "IHCD" )
/* = Call( ref, portNumber, writeConfigStr, fileName )
/*
/* where
/* portNumber = number of port (1 = modem, 2 = printer)
/* writeConfigStr = configuration of communications protocol, etc
/* fileName = name of text file to send
/*
/***********************************************************************/
/***********************************************************************/
/*
/* D E F I N E S
/*
/***********************************************************************/
#define ROUTINE_NAME "serial.writeFile"
#define hNIL 0L
#define pNIL 0L
#define kcchBuff 64
/***********************************************************************/
/*
/* I N C L U D E S
/*
/***********************************************************************/
#include "serial.h"
#include "error.h"
#include "get_port.h"
#include "absorb_echo.h"
#include "get_write_flags.h"
/***********************************************************************/
/*
/* P R O T O T Y P E S
/*
/***********************************************************************/
short open_file( char *pstFilename );
void close_file( short refFile, char *pch );
OSErr get_character( short refFile, char *pch, char *pchBuff, unsigned short *puichBuff );
/***********************************************************************/
/*
/* main
/*
/***********************************************************************/
pascal short main( port, pszConfig, pstFilename )
unsigned short port; /* serial port to use */
register char *pszConfig; /* communications configuration string */
register char *pstFilename; /* name of text file to send */
{
register OSErr err; /* result code from Toolbox routines */
ParamBlockRec param; /* parameter block for read/write */
Boolean fAddLF = false; /* flag for adding linefeeds */
Boolean fIgnore = false; /* flag for ignoring escape chars */
Boolean fStrip8Bit = false; /* flag for stripping high bit */
Boolean fAbsorbEcho = false; /* flag for waiting for echo from host */
short refIn; /* reference number for input port */
short refOut; /* reference number for output port */
register long ich; /* index in characters to send (# already sent) */
long cch; /* count of total characters to send */
long cchBuff = 0; /* number of characters waiting in read buffer */
char ch; /* character to write */
char *pch; /* buffer used in file reading */
unsigned short ichBuff = 0; /* index in buffer */
short refFile; /* reference number for file */
RememberA0();
SetUpA4();
if( pszConfig == pNIL ) {
display_error( "The second parameter must be a configuration string." );
RestoreA4( );
return( errParam );
}
if( pstFilename == pNIL ) {
display_error( "The third parameter must be a file name." );
RestoreA4( );
return( errParam );
}
if( *pstFilename == 0 ) {
display_error( "The third parameter must be a file name." );
RestoreA4( );
return( errParam );
}
err = get_port( port, &refIn, &refOut );
if( err != noErr ) {
display_error( "Illegal port number." );
RestoreA4( );
return( err );
}
pch = NewPtr( kcchBuff );
if( pch == pNIL ) {
display_error( "Not enough memory to allocate buffer." );
RestoreA4( );
return( errMemory );
}
ichBuff = 0;
if( ( refFile = open_file( pstFilename ) ) == 0 ) {
RestoreA4( );
return( errDiskRead );
}
get_write_flags( pszConfig, &fAddLF, &fIgnore, &fStrip8Bit, &fAbsorbEcho );
if( fAbsorbEcho ) { /* flush input buffer */
do {
err = SerGetBuf( refIn, &cchBuff );
if( err != noErr ) {
display_error( "Error trying to count buffer." );
break;
}
if( cchBuff != 0 ) {
param.ioParam.ioReqCount = 1;
param.ioParam.ioBuffer = &ch;
param.ioParam.ioRefNum = refIn;
err = PBRead( ¶m, false );
if( err != noErr ) {
display_error( "Error trying to flush the input buffer." );
return( errSerialRead );
}
}
} while( cchBuff != 0 );
}
ich = 0;
err = GetEOF( refFile, &cch );
if( err != noErr ) {
display_error( "Error in finding end of file." );
close_file( refFile, pch );
RestoreA4( );
return( errDiskRead );
}
while( ich < cch ) {
err = get_character( refFile, &ch, pch, &ichBuff );
if( err != noErr ) {
close_file( refFile, pch );
RestoreA4( );
return( err );
}
if( fStrip8Bit ) {
ch &= 0x7f;
}
param.ioParam.ioReqCount = 1;
param.ioParam.ioBuffer = &ch;
param.ioParam.ioRefNum = refOut;
err = PBWrite( ¶m, false );
if( err != noErr ) {
display_error( "Error writing to serial port." );
close_file( refFile, pch );
RestoreA4();
return( errSerialWrite );
}
if( fAbsorbEcho ) {
if( absorb_echo( refIn, ch ) != noErr ) {
display_error( "Error reading echo from serial port." );
close_file( refFile, pch );
RestoreA4();
return( errSerialWrite );
}
}
if( (ch == kchReturn) && (fAddLF) ) {
ch = kchLinefeed;
param.ioParam.ioReqCount = 1;
param.ioParam.ioBuffer = &ch;
param.ioParam.ioRefNum = refOut;
err = PBWrite( ¶m, false );
if( err != noErr ) {
display_error( "Error writing linefeed to serial port." );
close_file( refFile, pch );
RestoreA4();
return( errSerialWrite );
}
if( fAbsorbEcho ) {
if( absorb_echo( refIn, ch ) != noErr ) {
display_error( "Error reading echo from serial port." );
close_file( refFile, pch );
RestoreA4();
return( errSerialWrite );
}
}
}
ich++;
}
close_file( refFile, pch );
RestoreA4();
return( noErr );
}
/***********************************************************************/
/*
/* open_file
/*
/***********************************************************************/
short open_file( pstFilename )
char *pstFilename; /* name of text file to send */
{
short refFile; /* reference number for file */
OSErr err; /* result code from Toolbox routines */
ParamBlockRec param; /* parameter block for read/write */
param.ioParam.ioNamePtr = (StringPtr) pstFilename;
param.ioParam.ioVRefNum = 0;
param.ioParam.ioPermssn = fsRdPerm;
param.ioParam.ioMisc = 0;
err = PBOpen( ¶m, false );
refFile = param.ioParam.ioRefNum;
if( err == fnfErr ) {
display_error( "File not found." );
return( 0 );
} else if( err != noErr ) {
display_error( "Error in reading file." );
return( 0 );
}
err = SetFPos( refFile, fsFromStart, 0L );
if( err != noErr ) {
display_error( "Error in setting mark to beginning of file." );
FSClose( refFile );
return( 0 );
}
return( refFile );
}
/***********************************************************************/
/*
/* close_file
/*
/***********************************************************************/
void close_file( refFile, pch )
short refFile; /* reference number for file */
char *pch; /* buffer used in file reading */
{
OSErr err; /* result code from Toolbox routines */
err = FSClose( refFile );
if( err != noErr ) {
display_error( "Error closing file." );
}
DisposPtr( pch );
}
/***********************************************************************/
/*
/* get_character
/*
/***********************************************************************/
OSErr get_character( refFile, pch, pchBuff, puichBuff )
short refFile; /* reference number for file */
char *pch; /* character to get from file */
char *pchBuff; /* buffer used in file reading */
unsigned short *puichBuff; /* index in our file buffer */
{
OSErr err; /* result code from Toolbox routines */
long cch; /* count of characters to read */
if( *puichBuff >= kcchBuff ) /* if we have read the whole buffer */
*puichBuff = 0;
if( *puichBuff > 0 ) { /* if there are chars left in the buffer */
*pch = pchBuff[*puichBuff];
( *puichBuff )++;
} else { /* otherwise */
cch = kcchBuff;
err = FSRead( refFile, &cch, pchBuff ); /* read in a bunch more */
if( (err == eofErr) || (err == noErr) ) {
*puichBuff = 1;
*pch = *pchBuff;
} else {
display_error( "Error in reading from file." );
return( errDiskRead );
}
}
return( noErr );
}
#include "get_write_flags.c"
#include "get_port.c"
#include "absorb_echo.c"